//package es.buffygilmore.gastosapp;
//
//import java.io.File;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
//import android.app.Activity;
//import android.content.Context;
//import android.content.Intent;
//import android.content.pm.PackageManager;
//import android.content.pm.ResolveInfo;
//import android.database.Cursor;
//import android.net.Uri;
//import android.os.Bundle;
//import android.os.Environment;
//import android.provider.MediaStore;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.ImageView;
//
//public class GastoDetalle extends Activity {
//
//	private static final int REQUEST_IMAGE_CAPTURE = 1;
//
//	private static final String JPEG_FILE_PREFIX = "IMG_";
//	private static final String JPEG_FILE_SUFFIX = ".jpg";
//
//	private EditText mTitleText;
//	private EditText mDescripcionText;
//	private Long mRowId;
//	private Long mIdColeccion;
//	private CollectionAppDbAdapter mDbHelper;
//
//	private ImageView mImageView;
//
//	private String mCurrentPhotoPath;
//
//	@Override
//	protected void onCreate(final Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		this.mDbHelper = new CollectionAppDbAdapter(this);
//
//		this.mDbHelper.open();
//
//		this.setContentView(R.layout.edicion_elemento);
//		this.setTitle(R.string.edit_note);
//
//		this.mTitleText = (EditText) this.findViewById(R.id.titulo_elemento);
//		this.mDescripcionText = (EditText) this.findViewById(R.id.descripcion_elemento);
//
//		final Button confirmButton = (Button) this.findViewById(R.id.guardar_elemento);
//
//		this.mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState
//				.getSerializable(CollectionAppDbAdapter.KEY_ELEMENTO_ROWID);
//		final Bundle extras = this.getIntent().getExtras();
//		if (this.mRowId == null) {
//			this.mRowId = extras != null
//					&& extras.getLong(CollectionAppDbAdapter.KEY_ELEMENTO_ROWID) != 0 ? extras
//							.getLong(CollectionAppDbAdapter.KEY_ELEMENTO_ROWID) : null;
//		}
//
//		// Recupera el idColeccion
//		this.mIdColeccion = extras != null ? extras
//				.getLong(CollectionAppDbAdapter.KEY_ELEMENTO_COLECCION) : null;
//		this.mImageView = (ImageView) this.findViewById(R.id.vista_foto);
//
//		final Button picSBtn = (Button) this.findViewById(R.id.sacar_foto);
//		this.setBtnListenerOrDisable(picSBtn, this.mTakePicSOnClickListener,
//				MediaStore.ACTION_IMAGE_CAPTURE);
//
//		this.populateFields();
//
//		confirmButton.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(final View view) {
//				GastoDetalle.this.setResult(RESULT_OK);
//				GastoDetalle.this.finish();
//			}
//
//		});
//
//	}
//
//	private void populateFields() {
//		if (this.mRowId != null) {
//			final Cursor elemento = this.mDbHelper.obtenerElemento(this.mRowId);
//			this.startManagingCursor(elemento);
//			this.mTitleText.setText(elemento.getString(elemento
//					.getColumnIndexOrThrow(CollectionAppDbAdapter.KEY_ELEMENTO_NOMBRE)));
//			this.mDescripcionText.setText(elemento.getString(elemento
//					.getColumnIndexOrThrow(CollectionAppDbAdapter.KEY_ELEMENTO_COMENTARIO)));
//			this.mCurrentPhotoPath = elemento.getString(elemento
//					.getColumnIndexOrThrow(CollectionAppDbAdapter.KEY_ELEMENTO_RUTA_IMAGEN));
//			if (this.mCurrentPhotoPath != null && !"".equals(this.mCurrentPhotoPath)) {
//
//				ImagenHelper.setPic(this.mImageView, this.mCurrentPhotoPath);
//			}
//		}
//	}
//
//	@Override
//	protected void onSaveInstanceState(final Bundle outState) {
//		super.onSaveInstanceState(outState);
//		this.saveState();
//		outState.putSerializable(CollectionAppDbAdapter.KEY_ELEMENTO_ROWID, this.mRowId);
//	}
//
//	@Override
//	protected void onPause() {
//		super.onPause();
//		this.saveState();
//	}
//
//	@Override
//	protected void onResume() {
//		super.onResume();
//		this.populateFields();
//	}
//
//	private void saveState() {
//		final Elemento elemento = new Elemento();
//		elemento.setNombre(this.mTitleText.getText().toString());
//		elemento.setComentario(this.mDescripcionText.getText().toString());
//		elemento.setPropiedad(1);
//		elemento.setIdColeccion(this.mIdColeccion);
//		elemento.setRutaImagen(this.mCurrentPhotoPath);
//
//		if (this.mRowId == null) {
//			final long id = this.mDbHelper.crearElemento(elemento);
//			if (id > 0) {
//				this.mRowId = id;
//			}
//		} else {
//			this.mDbHelper.modificarElemento(this.mRowId, elemento);
//		}
//	}
//
//	private void dispatchTakePictureIntent() {
//		final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//		if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
//			File f = null;
//
//			try {
//				f = this.setUpPhotoFile();
//				this.mCurrentPhotoPath = f.getAbsolutePath();
//				takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
//			} catch (final IOException e) {
//				e.printStackTrace();
//				f = null;
//				this.mCurrentPhotoPath = null;
//			}
//			this.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
//		}
//	}
//
//	@Override
//	protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
//		if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//			// final Bundle extras = data.getExtras();
//			// final Bitmap imageBitmap = (Bitmap) extras.get("data");
//			// this.mImageView.setImageBitmap(imageBitmap);
//			this.handleBigCameraPhoto();
//		}
//	}
//
//	Button.OnClickListener mTakePicSOnClickListener = new Button.OnClickListener() {
//		@Override
//		public void onClick(final View v) {
//			GastoDetalle.this.dispatchTakePictureIntent();
//		}
//	};
//
//	private void setBtnListenerOrDisable(final Button btn,
//			final Button.OnClickListener onClickListener, final String intentName) {
//		if (isIntentAvailable(this, intentName)) {
//			btn.setOnClickListener(onClickListener);
//		} else {
//			btn.setText(this.getText(R.string.cannot).toString() + " " + btn.getText());
//			btn.setClickable(false);
//		}
//	}
//
//	/**
//	 * Indicates whether the specified action can be used as an intent. This method queries the
//	 * package manager for installed packages that can respond to an intent with the specified
//	 * action. If no suitable package is found, this method returns false.
//	 * http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
//	 *
//	 * @param context The application's environment.
//	 * @param action The Intent action to check for availability.
//	 *
//	 * @return True if an Intent with the specified action can be sent and responded to, false
//	 *         otherwise.
//	 */
//	public static boolean isIntentAvailable(final Context context, final String action) {
//		final PackageManager packageManager = context.getPackageManager();
//		final Intent intent = new Intent(action);
//		final List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
//				PackageManager.MATCH_DEFAULT_ONLY);
//		return list.size() > 0;
//	}
//
//	private File setUpPhotoFile() throws IOException {
//
//		final File f = this.createImageFile();
//		this.mCurrentPhotoPath = f.getAbsolutePath();
//
//		return f;
//	}
//
//	private File createImageFile() throws IOException {
//		// Create an image file name
//		final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//		final String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
//		final File storageDir = Environment
//				.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//
//		final File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, storageDir);
//		return imageF;
//	}
//
//	private void handleBigCameraPhoto() {
//
//		if (this.mCurrentPhotoPath != null) {
//			ImagenHelper.setPic(this.mImageView, this.mCurrentPhotoPath);
//			this.galleryAddPic();
//			this.mCurrentPhotoPath = null;
//		}
//
//	}
//
//	private void galleryAddPic() {
//		final Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
//		final File f = new File(this.mCurrentPhotoPath);
//		final Uri contentUri = Uri.fromFile(f);
//		mediaScanIntent.setData(contentUri);
//		this.sendBroadcast(mediaScanIntent);
//	}
// }
